<template>
  <el-container class="layout-container">
    <el-aside width="200px">
      <div class="el-aside__logo"></div>
      <el-menu
          active-text-color="#ffd04b"
          background-color="#232323"
          text-color="#fff"
          router
      >
        <el-menu-item index="/homework/publish" v-if="userInfoStore.info.type === '教师'">
          <el-icon><Management /></el-icon>
          <span>作业发布</span>
        </el-menu-item>
          <el-menu-item index="/homework/list">
              <el-icon>
                  <Management />
              </el-icon>
              <span>作业列表</span>
          </el-menu-item>
        <el-menu-item index="/information/publish"  v-if="userInfoStore.info.type === '管理员'">
          <el-icon><Promotion /></el-icon>
          <span>信息发布</span>
        </el-menu-item>
        <el-menu-item index="/information/list"><!-- 都可以看 -->
          <el-icon><Promotion /></el-icon>
          <span>信息列表</span>
        </el-menu-item>
        <el-menu-item index="/view/category"  v-if="userInfoStore.info.type === '管理员'">
          <el-icon><Management /></el-icon>
          <span>用户管理</span>
        </el-menu-item>
        <el-menu-item index="/view/class"  v-if="userInfoStore.info.type === '管理员'">
          <el-icon><Management /></el-icon>
          <span>班级管理</span>
        </el-menu-item>
        <el-menu-item index="/view/teacher"  v-if="userInfoStore.info.type === '管理员'">
          <el-icon><Management /></el-icon>
          <span>教师管理</span>
        </el-menu-item>
        <el-sub-menu index="personal-center"><!-- 都可以看 -->
          <template #title>
            <el-icon><UserFilled /></el-icon>
            <span>个人中心</span>
          </template>
          <el-menu-item index="/user/patriarch"  v-if="userInfoStore.info.type === '家长'">
            <el-icon><User /></el-icon>
            <span>家长个人信息</span>
          </el-menu-item>
          <el-menu-item index="/user/patriarchChild" v-if="userInfoStore.info.type === '家长'">
            <el-icon><User /></el-icon>
            <span>家长关联子女管理</span>
          </el-menu-item>
          <el-menu-item index="/user/student" v-if="userInfoStore.info.type === '学生'">
            <el-icon><User /></el-icon>
            <span>学生个人信息</span>
          </el-menu-item>
          <el-menu-item index="/user/teacher" v-if="userInfoStore.info.type === '教师'">
            <el-icon><User /></el-icon>
            <span>教师个人信息</span>
          </el-menu-item>
          <el-menu-item index="/user/resetPassword"><!-- 都可以看 -->
            <el-icon><EditPen /></el-icon>
            <span>重置密码</span>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <div>用户：<strong>{{ userInfoStore.info.nickname }}</strong></div>
        <el-dropdown placement="bottom-end" @command="handleCommand">
          <span class="el-dropdown__box">
            <el-avatar :src="userInfoStore.info.userPic ? userInfoStore.info.userPic : avatar" />
            <el-icon><CaretBottom /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>

              <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
              <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
      <el-footer></el-footer>
    </el-container>
  </el-container>
</template>

<script setup>
import {
  Management,
  Promotion,
  UserFilled,
  User,
  EditPen,
  SwitchButton,
  CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'

import {getTeacherInfoService, userInfoService} from '@/api/user.js'
import useUserInfoStore from '@/stores/userInfo.js'
import { useTokenStore } from '@/stores/token.js'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const tokenStore = useTokenStore();
const userInfoStore = useUserInfoStore();
const router = useRouter();

const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm(
        '您确认要退出吗?',
        '温馨提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }
    ).then(async () => {
      tokenStore.removeToken()
      userInfoStore.removeInfo()
      router.push('/login')
      ElMessage({
        type: 'success',
        message: '退出登录成功',
      })
    }).catch(() => {
      ElMessage({
        type: 'info',
        message: '用户取消了退出登录',
      })
    })
  } else {
    router.push('/user/' + command)
  }
}
</script>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;

  .el-aside {
    background-color: #232323;

    &__logo {
      height: 120px;
      background: url('@/assets/logo.png') no-repeat center / 120px auto;
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dropdown__box {
      display: flex;
      align-items: center;

      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}
</style>
